local SelGenMuty = class("SelGenMuty")
local HobbyGenPool = require "packages/lang/HobbyGenPool"
SelGenMuty.COMMAND = "MiniGame"
SelGenMuty.GAMETYPE = "seeothersel"
SelGenMuty.POOLTYPE = {
  Players ="Players",
  AIs = "AIs"
}
function SelGenMuty:initialize(data)
  self.room = Fk:currentRoom()
  self.generalNum = data.generalNum
  self.repcount = data.repcount
  self.istest = data.istest
  self.openhobbygens = data.openhobbygens
  self.genpoolname = data.genpoolname
  self.aipoolname = data.aipoolname or data.genpoolname
  if not data.closeskin then
    self:openIsSeeSkin()
  end
  if data.openhobbygens then
    self:selGensByHobby()
  end
  self:openMutySelGens()
end

--打开多人选将面板
function SelGenMuty:openMutySelGens()
  local room = self.room
  local command,game_type = SelGenMuty.COMMAND,SelGenMuty.GAMETYPE
  local game = Fk.mini_games[game_type]
  if not game then return end

  local realplayers = {}
  local aigenpool = HobbyGenPool:getgenPool(self.aipoolname,SelGenMuty.POOLTYPE.AIs)
  local aigens = table.random(aigenpool,8*5)
  local players = room.players
  local playergens
  if not self.openhobbygens then
    playergens = table.clone(HobbyGenPool:getgenPool(self.genpoolname,SelGenMuty.POOLTYPE.Players))
    table.shuffle(playergens)
  end
  for _,pr in ipairs(players) do
    if pr.id > 0 then
      local repgens = {}
      local data = {seat = pr.seat.."号",gens ={},othergens = {},otherseats={},repcount = self.repcount,repgens = repgens }
      local generals = self.openhobbygens and pr.selgens or playergens
      for i = 1,self.generalNum do
        if #generals > 0 then
          table.insert(data.gens,table.remove(generals,1))
        end
      end
      if self.istest and pr.id > 0 then
        table.insert(data.gens,1,self.istest)
      end
      for i = 1,data.repcount do
        if #generals > 0 then
          table.insert(data.repgens,table.remove(generals,1))
        end
      end
      pr.mini_game_data = { type = game_type, data = data}
      table.insert(realplayers,pr)
    else
      -- pr.default_reply = json.encode("") --忠臣反贼人机将池
      pr.default_reply = json.encode(table.remove(aigens,1)) --忠臣反贼人机将池
    end     
  end
  room.aiselgens = aigens

  for _,pr in ipairs(realplayers) do
    if pr.id > 0 and pr.mini_game_data then
      if #realplayers == 1 then
        -- table.insert(pr.mini_game_data.data.othergens,pr.mini_game_data.data.gens)
        -- table.insert(pr.mini_game_data.data.othergens,pr.mini_game_data.data.gens)
        -- table.insert(pr.mini_game_data.data.othergens,pr.mini_game_data.data.gens)
        pr.request_data = json.encode(pr.mini_game_data)
        pr.default_reply = game.default_choice and json.encode(game.default_choice(pr, pr.mini_game_data.data)) or ""
      else
        for i, friend in ipairs(realplayers) do
          if pr ~= friend and friend.id > 0 and friend.mini_game_data then
            table.insert(pr.mini_game_data.data.othergens,friend.mini_game_data.data.gens)
            table.insert(pr.mini_game_data.data.otherseats,friend.mini_game_data.data.seat)
          end
        end
        pr.request_data = json.encode(pr.mini_game_data)
        pr.default_reply = game.default_choice and json.encode(game.default_choice(pr, pr.mini_game_data.data)) or ""
      end
    end
  end

  room:notifyMoveFocus(realplayers, "AskForGeneral")
  room:doBroadcastRequest(command, realplayers)

  for _, p in ipairs(players) do
    p.mini_game_data = nil
    if not p.reply_ready then
      p.client_reply = p.default_reply
      p.reply_ready = true
    end
  end

  for _, pr in ipairs(players) do
    if pr.general == "" and pr.reply_ready then
      local general = json.decode(pr.client_reply)
      room:setPlayerGeneral(pr, general,true)
    else
      room:setPlayerGeneral(pr, "blank_shibing",true)
    end
    pr.default_reply = ""
  end
  room:askForChooseKingdom(players)
  for _, p in ipairs(players) do
    room:broadcastProperty(p, "general")
    room:broadcastProperty(p, "kingdom")
  end
end

--打开观看皮肤
function SelGenMuty:openIsSeeSkin()
  local room = self.room
  self:openMutyDetailedCheck("#skinhobby",1,1,{{type=1,name="seeskin"},{type=1,name="noskin"}})
  for i, pr in ipairs(room.players) do
    if pr.id > 0 then
      local selchs = pr.client_reply
      if selchs == "seeskin" then pr.seeskin = true end
    end
  end
end
--打开共享多选选项界面
function SelGenMuty:openMutyDetailedCheck(name,min,max,chc)
  local room = self.room
  local command,game_type = SelGenMuty.COMMAND,"mutyselchc"
  local game = Fk.mini_games[game_type]
  if not game then return end
  local realplayers = {}
  for _, pr in ipairs(room.players) do
    if pr.id > 0 then
      local data = {skillname=name,minnum=min,maxnum=max,choices=chc} 
      pr.mini_game_data = { type = game_type, data = data}
      pr.request_data = json.encode(pr.mini_game_data)
      pr.default_reply = game.default_choice and json.encode(game.default_choice(pr, pr.mini_game_data.data)) or ""
      table.insert(realplayers,pr)
    end 
  end
  room:notifyMoveFocus(realplayers, "AskForChoice")
  room:doBroadcastRequest(command, realplayers)
  for _, pr in ipairs(realplayers) do
    pr.mini_game_data = nil
    if not pr.reply_ready then
      pr.client_reply = pr.default_reply
    end
    local dedata = json.decode(pr.client_reply)
    if dedata and #dedata == 1 then
      pr.client_reply = dedata[1].name
    end
    pr.default_reply = ""
  end
end

--根据爱好筛选将池
function SelGenMuty:selGensByHobby()
  local room = self.room
  local hobbygens = {gender ={{},{}}}
  local totalnum = self.generalNum + self.repcount
  
  local playerGens = HobbyGenPool:getgenPool(self.genpoolname,SelGenMuty.POOLTYPE.Players)
  for _,gen in ipairs(playerGens) do
    local gender = Fk.generals[gen].gender
    local gendergens = hobbygens.gender[gender]
    if gendergens then
      table.insert(hobbygens.gender[gender],gen)
    end
  end

  self:openMutyDetailedCheck("#selhobby",1,1,{{type=1,name="randsel",price=20},{type=1,name="allmale",price=30},{type=1,name="allfemale",price=40}})

  table.shuffle(hobbygens.gender[1])
  table.shuffle(hobbygens.gender[2])
  local splitgens = function(pr,filtergens,num)
    pr.selgens = {}
    if not filtergens or #filtergens == 0 then
      for i=1,num do
        local swithgender = i % 2
        if swithgender == 0 then
          swithgender = 2
        end
        local switchgens = hobbygens.gender[swithgender]
        if #switchgens > 0 then         
          table.insert(pr.selgens,table.remove(switchgens,1))
        end
      end
    else
      for i=1,num do
        table.insert(pr.selgens,table.remove(filtergens,1))
      end
    end
  end
  local hobbychoices = {allmale = 1,allfemale = 2}
  for i, pr in ipairs(room.players) do
    if pr.id > 0 then
      local selchs = pr.client_reply
      splitgens(pr, hobbygens.gender[hobbychoices[selchs]],totalnum)
    end
  end
end

return SelGenMuty